package org.book.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.book.bean.Book;
import org.book.bean.Borrow;
import org.book.bean.Manager;
import org.book.bean.User;
import org.book.common.JsonResult;
import org.book.configuration.aop.NoCheckLogin;
import org.book.configuration.aop.PermissionCheck;
import org.book.service.IManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dancun
 * @since 2021-03-05
 */
@Slf4j
@RestController
@RequestMapping("/manager")
public class ManagerController {
    @Autowired
    IManagerService managerService;

    @RequestMapping(value = "login")
    @NoCheckLogin
    public JsonResult login (Manager manager, HttpServletRequest request){
        try{
            QueryWrapper<Manager> wrapper = Wrappers.query();
            wrapper.eq("username",manager.getUsername());
            wrapper.eq("password",manager.getPassword());
            Manager loginuser = managerService.getOne(wrapper);
            if(loginuser==null||loginuser.getUsername()==null||loginuser.getUsername().equals("")){
                return  JsonResult.renderFail("账号或密码错误");
            }
            request.getSession().setAttribute("user",loginuser.getUsername());
            loginuser.setPassword("");
            return  JsonResult.renderSuccess("登陆成功",loginuser);
        }catch (Exception e){
           e.printStackTrace();
            return  JsonResult.renderFail("服务器异常");
        }

    }

    @RequestMapping(value = "exit")
    public JsonResult exit( HttpServletRequest request){
        request.getSession().setAttribute("user",null);
        return JsonResult.renderSuccess();
    }

    @PostMapping("/list")
    @NoCheckLogin
    public JsonResult list(String key,Integer pageNum, Integer pageSize){

        if(pageSize!=null&&pageSize!=0){
            if (pageNum==null){
                pageSize=0;
            }
            PageHelper.startPage(pageNum,pageSize);
        }
        PageInfo page = null;
        try{
            QueryWrapper<Manager> wrapper = Wrappers.query();
            if(key!=null&&!key.equals("")){
                wrapper.like("tel" ,key).or().like("username",key);
            }
            page = new PageInfo<Manager>(managerService.list(wrapper));
        }catch (Exception e){
            return  JsonResult.renderError(e.toString());
        }

        return JsonResult.renderSuccess(page);

    }
    @PostMapping("/findById")
    public JsonResult findById(String isbn){
        Manager manager = null;
        try{
            manager = managerService.getById(isbn);
        }catch (Exception e){
            log.info(e.getStackTrace().toString());
            return JsonResult.renderError(e.getStackTrace().toString());
        }
        return JsonResult.renderSuccess(manager);
    }

    @PostMapping("/search")
    public JsonResult search(String key,Integer pageNum,Integer pageSize ) {


        if (pageSize != null && pageSize != 0) {
            if (pageNum == null) {
                pageSize = 0;
            }
            PageHelper.startPage(pageNum, pageSize);
        }
        PageInfo page = null;
        try {
            QueryWrapper wrapper = Wrappers.query();
            wrapper.like("username" ,key);
            page = new PageInfo<Manager>(managerService.list(wrapper));
        } catch (Exception e) {
            return JsonResult.renderError(e.toString());
        }

        return JsonResult.renderSuccess(page);
    }
    @PostMapping("/insert")
    @PermissionCheck
    public JsonResult insert(Manager manager){
        try{
            managerService.save(manager);
            return JsonResult.renderSuccess("添加管理员成功");
        }catch (Exception e){
            return JsonResult.renderFail("添加管理员失败");
        }

    }

    @PostMapping("/update")
    @PermissionCheck
    public JsonResult update(Manager manager){
        try{
            managerService.updateById(manager);
            return JsonResult.renderSuccess("修改管理员信息成功");
        }catch (Exception e){
            return JsonResult.renderFail("修改管理员信息失败");
        }

    }

    @PostMapping("/delete")
    public JsonResult delete(Manager manager){
        try{
            managerService.removeById(manager.getUsername());
            return JsonResult.renderSuccess("删除成功");
        }catch (Exception e){
            return JsonResult.renderFail("删除失败");
        }

    }

}
